// pages/signUp/siginUp.js
import WxValidate from "./WxValidate.js"
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    taxType: 0,
    isReport: 1,
    formData: {
      taxType: 0,
      organization: "",
      taxNumber: "",
      bankAccount: "",
      mailingAddress: "",
      postcode: "",
      truename: "",
      sex: 1,
      position: "",
      phone: "",
      email: "",
      feeType: 1,
      isReport: 1,
      reportTitle: "",
      roomType: 1,
      payType: 1,
    },

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initValidate()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  //表单规则
  initValidate() {
    const rules = {
      taxType: {
        required: true
      },
      organization: {
        required: true
      },
      taxNumber: {
        required: true
      },
      postcode: {
        required: true
      },
      truename: {
        required: true

      },
      position: {
        required: true

      },
      phone: {
        required: true

      },
      email: {
        required: true

      },
      feeType: {
        required: true
      },

      roomType: {
        required: true
      },
      payType: {
        required: true

      }
    }
    const messages = {
      taxType: {
        required: "开票类型不能为空"
      },
      organization: {
        required: "单位不能为空"
      },
      taxNumber: {
        required: "税号不能为空"
      },
      mailingAddress: {
        required: "地址不能为空"
      },
      postcode: {
        required: "邮编不能为空"
      },
      truename: {
        required: "姓名不能为空"

      },
      position: {
        required: "职位不能为空"

      },
      phone: {
        required: "手机不能为空"

      },
      email: {
        required: "邮箱不能为空"

      },
      feeType: {
        required: "开票信息不能为空"
      },

      roomType: {
        required: "房型不能为空"
      },
      payType: {
        required: "支付形式不能为空"
      }

    }
    this.WxValidate = new WxValidate(rules, messages)
  },

  //表单提交
  formSubmit(e) {
    e.detail.value.meetingId = 2
    const parmas = e.detail.value
    if (!this.WxValidate.checkForm(parmas)) {
      const err = this.WxValidate.errorList[0]
      this.showErr(err)
      return false
    }
    this.showSuccess(parmas)
  },
  // 提交失败
  showErr(err) {
    wx.showModal({
      content: err.msg,
      showCancel: false
    })
  },
  // 提交成功
  showSuccess(parmas) {
    let that = this
    wx.request({
      url: app.globalData.url + '/fyhs-meeting/app/attenders/save',
      data: parmas,
      timeout: 3000,
      method: "POST",
      header: {
        'content-type': 'application/JSON'
      },
      success: function (res) {
        if (res.data.code === 0) {
          wx.showModal({
            content: "报名成功",
            showCancel: false,
            success: () => {
              console.log(res)
            }
          })






        } else {
          wx.showModal({
            content: res.data.msg,
            showCancel: false,
          })

        }
      },
    })
  },
  taxTypeChange(e) {
    let that = this
    let val = Number(e.detail.value)
    that.setData({
      taxType: val
    })
  },
  isReportChange(e) {
    let that = this
    let val = Number(e.detail.value)
    that.setData({
      isReport: val
    })
  }
})